<?php

class selectSliderWidget extends CWidget {

    public $formAction = "";
    public $inputId = "amount";
    public $slider_id = "slider-range";
    public $startid = "start_level";
    public $endid = "end_level";
    public $min = 1;
    public $max = 100;
    public $gameid, $reqUrl;
    public $value = "1,50";
    public $ptype = 'PL';

    public function run() {
        //注册需要的js
        $basePath = Yii::getPathOfAlias('application.extensions.selectSlider');
        $baseUrl = Yii::app()->getAssetManager()->publish($basePath);
        $cs = Yii::app()->getClientScript();
        $cs->registerCoreScript('jquery');
        $cs->registerCssFile($baseUrl . '/css/slider.css');
        $cs->registerScriptFile($baseUrl . '/js/slider.js', CClientScript::POS_END);
        //单选多选按钮判断  $vo->linkurl   $vo->imgurl
        $scriptStr = '
            $(function() {
                $( "#' . $this->slider_id . '" ).slider({
                    range: true,
                    min: ' . $this->min . ',
                    max: ' . $this->max . ',
                    values: [ ' . $this->value . ' ],
                    slide: function( event, ui ) {
                        $( "#' . $this->startid . '" ).val(ui.values[ 0 ]);
                        $( "#' . $this->endid . '" ).val(ui.values[ 1 ]);
                    },
                    stop:function(event, ui){
                         getAjaxVal(ui.values[ 0 ],ui.values[ 1 ])
                    }
                });
                 $( "#' . $this->startid . '" ).val($(  "#' . $this->slider_id . '" ).slider( "values", 0 ));
                 $( "#' . $this->endid . '" ).val($(  "#' . $this->slider_id . '" ).slider( "values", 1 ));
            });
            function changeLevel(obj){
                var jobj = $(obj);
                var sVal = $("#' . $this->startid . '").val();
                var eVal = $("#' . $this->endid . '").val();
                 if(eVal > ' . $this->max . '){
                      eVal = ' . $this->max . ';
                      $("#' . $this->endid . '").val(eVal);
                 }            
                if(eVal<= sVal){
                    alert("Start level is not greater than end level!");
                     jobj.focus();
                }else{
                    $(  "#' . $this->slider_id . '" ).slider("values",0,sVal);
                    $(  "#' . $this->slider_id . '" ).slider("values",1,eVal);
                     getAjaxVal(sVal,eVal);
                }
            }
        ';
        switch ($this->ptype) {
            case 'SKILL':
                $scriptStr .= '
                    function getAjaxVal(slevel,elevel){
                        var url = "' . $this->reqUrl . '";
                        var skill_id = $("#skill").val();
                        var attack = $("#attack_box").val();
                        var defence = $("#defence_box").val();
                        var strength = $("#strength_box").val();
                        var params = {s_level:slevel,e_level:elevel,skill_id:skill_id,attack:attack,defence:defence,strength:strength};
                        $.get(url,params,function(data){
                            $("#show_hour").val(data.hour);
                            $("#show_price").val(data.price);
                        },"json");
                    }';
                break;
            default:
                $scriptStr .= '
                    function getAjaxVal(slevel,elevel){
                        var url = "' . $this->reqUrl . '";
                        var params = {s_level:slevel,e_level:elevel};
                        $.get(url, params, function(data){
                            $("#show_hour").val(data.hour);
                            $("#show_price").val(data.price);
                        },"json");
                    }';
                break;
        }
        Yii::app()->clientScript->registerScript($this->slider_id, $scriptStr, CClientScript::POS_END);
    }

}